#import "FontSizeViewController.h"

@implementation FontSizeViewController

- (id)init {
    self = [ super init ];
    if (self != nil) {
        fonts = [ [ NSMutableArray alloc ] init ];
		[fonts addObject: @"American Typewriter"];
		[fonts addObject: @"Apple Gothic"];
		[fonts addObject: @"Arial"];
		[fonts addObject: @"Arial Rounded MT Bold"];
		[fonts addObject: @"Courier"];
		[fonts addObject: @"Courier New"]; 
		[fonts addObject: @"Georgia"];
		[fonts addObject: @"Helvetica"];
		[fonts addObject: @"Helvetica Neue"];
		[fonts addObject: @"Marker Felt"];
		[fonts addObject: @"Times"];
		[fonts addObject: @"Times New Roman"];
		[fonts addObject: @"Trebuchet MS"];
		[fonts addObject: @"Verdana"];
		[fonts addObject: @"Zapfino"];

        sizes = [ [ NSMutableArray alloc ] init ];
        [ sizes addObject: @"Small" ];
        [ sizes addObject: @"Medium" ];
        [ sizes addObject: @"Large" ];
        [ sizes addObject: @"Super-Size" ];

        selection[0] = selection[1] = 0;
    }
    return self;
}

- (void)loadView {
    CGRect bounds = [ [ UIScreen mainScreen ] applicationFrame ];

    [ super loadView ];

    pickerView = [ [ UIPickerView alloc ]
        initWithFrame: CGRectMake(0.0, bounds.size.height - 216.0, 0.0, 0.0)
    ];

    pickerView.delegate = self;
    pickerView.dataSource = self;
    pickerView.showsSelectionIndicator = YES;
    [ self.view addSubview: pickerView ];

    textView = [ [ UITextView alloc ] initWithFrame:
        CGRectMake(0.0, 0.0, bounds.size.width, bounds.size.height - 216.0)
    ];

    textView.font = [ UIFont fontWithName: @"Arial" size: 18.0 ];
    textView.textAlignment = UITextAlignmentLeft;
    textView.editable = NO;

    [ self updateText ];
    [ self.view addSubview: textView ];
}

- (void)updateText {
    textView.text = [ NSString stringWithFormat:
        @"One %@, %@ schnoz:\nComing right up!\n",
        [ sizes objectAtIndex: selection[1] ],
        [ fonts objectAtIndex: selection[0] ]
    ];
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 2;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    switch(component) {
        case(0):
            return [ fonts count ];
            break;
        case(1):
            return [ sizes count ];
            break;
    }
    return 0;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    switch (component) {
        case(0):
            return [ fonts objectAtIndex: row ];
            break;
        case(1):
            return [ sizes objectAtIndex: row ];
    }
    return nil;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    selection[component] = row;
    [ self updateText ];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return NO;
}


- (void)didReceiveMemoryWarning {
    [ super didReceiveMemoryWarning ];
}

- (void)dealloc {
    [ pickerView release ];
    [ fonts release ];
    [ sizes release ];
    [ super dealloc ];
}

@end

